<?php
class ModelCustomerCustomerGrade extends Model {
	public function addCustomerGrade($data) {
		$name = $this->db->escape($data['name']);
		$sql = "INSERT INTO " . DB_PREFIX . "customer_grade SET `name` = '" . $name;
		if($data['image']){
			$sql .= "', image = '" . $this->db->escape(html_entity_decode($data['image'], ENT_QUOTES, 'UTF-8'));
		}
		$sql .= "', grade = '" . (int)$data['grade'] . 
			"', min_point = '" . (int)$data['min_point'] . 
			"', max_point = '" . (int)$data['max_point'] . 
			"', discount = '" . (float)$data['discount'] . "'";
		$this->db->query($sql);
		$customer_grade_id = $this->db->getLastId();
		
		foreach ($data['customer_grade_description'] as $language_id => $value) {
			$sql_desc = "INSERT INTO " . DB_PREFIX . "customer_grade_description SET customer_grade_id = '" . (int)$customer_grade_id . 
				"', language_id = '" . (int)$language_id . 
				"', display_name = '";
			if($value['display_name']){
				$sql_desc .= $this->db->escape($value['display_name']);
			}else{
				$sql_desc .= $name;
			}
			$sql_desc .= "', description = '" . $this->db->escape($value['description']) . "'";
			$this->db->query($sql_desc);
		}	
	}
	
	public function editCustomerGrade($customer_grade_id, $data) {
		$name = $this->db->escape($data['name']);
		$sql = "UPDATE " . DB_PREFIX . "customer_grade SET `name` = '" . $name . "', image = ";
		if($data['image']){
			$sql .= "'" . $this->db->escape(html_entity_decode($data['image'], ENT_QUOTES, 'UTF-8')) . "'";
		}else{
			$sql .= "null";
		}
		$sql .= ", grade = '" . (int)$data['grade'] . 
			"', min_point = '" . (int)$data['min_point'] . 
			"', max_point = '" . (int)$data['max_point'] . 
			"', discount = '" . (float)$data['discount'] . 
			"' WHERE customer_grade_id = '" . (int)$customer_grade_id . "'";
		$this->db->query($sql);
	
		$this->db->query("DELETE FROM " . DB_PREFIX . "customer_grade_description WHERE customer_grade_id = '" . (int)$customer_grade_id . "'");
		foreach ($data['customer_grade_description'] as $language_id => $value) {
			$sql_desc = "INSERT INTO " . DB_PREFIX . "customer_grade_description SET customer_grade_id = '" . (int)$customer_grade_id . 
				"', language_id = '" . (int)$language_id . 
				"', display_name = '";
			if($value['display_name']){
				$sql_desc .= $this->db->escape($value['display_name']);
			}else{
				$sql_desc .= $name;
			}
			$sql_desc .= "', description = '" . $this->db->escape($value['description']) . "'";
			$this->db->query($sql_desc);
		}
	}
	
	public function getCustomerGrade($customer_grade_id) {
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "customer_grade WHERE customer_grade_id = '" . (int)$customer_grade_id . "'");
		return $query->row;
	}
	
	public function getCustomerGradeDescriptions($customer_grade_id) {
		$customer_grade_data = array();
		
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "customer_grade_description WHERE customer_grade_id = '" . (int)$customer_grade_id . "'");
		foreach ($query->rows as $result) {
			$customer_grade_data[$result['language_id']] = array(
				'display_name' => $result['display_name'],
				'description'  => $result['description']
			);
		}
		
		return $customer_grade_data;
	}
	
	public function getCustomerGrades() {
		$sql = "SELECT cg.*, cgd.display_name FROM " . 
			DB_PREFIX . "customer_grade cg LEFT JOIN " . 
			DB_PREFIX . "customer_grade_description cgd ON cg.customer_grade_id = cgd.customer_grade_id AND cgd.language_id = '" . 
			(int)$this->config->get('config_language_id') . "' ORDER BY cg.`grade`";
			
		$query = $this->db->query($sql);
		
		return $query->rows;
	}
}
?>